package wfl.spec.edu;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;



public class ZhinengActivity extends AppCompatActivity {
    private static final String TAG ="ZhinengActivity";
    public String temperature;
    public String humidity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhineng);
        ws();
        Button button1= (Button) findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(ZhinengActivity.this,NewsActivity.class);
                startActivity(intent);
            }
        });
        Button button2= (Button) findViewById(R.id.button_2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(ZhinengActivity.this,MajorActivity.class);
                startActivity(intent);
            }
        });
        Button button4= (Button) findViewById(R.id.button_4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(ZhinengActivity.this,GuanyuActivity.class);
                startActivity(intent);
            }
        });

    }
    public void ws(){
        new Thread(new Runnable(){
            @Override
            public void run() {
                while (true) {
                    try {
                        //客户端，相当于浏览器里输入地址
                        OkHttpClient client = new OkHttpClient();
                        Request request = new Request
                                .Builder()
                                .url("http://www.yyjsx.xyz/api/iot/readHumiture ")
                                .build();
                        //发送请求 相当于在浏览器中点回车
                        Response response = client.newCall(request).execute();
                        String body = response.body().string();
                        Log.d(TAG, body);
                        JSONObject jobj = new JSONObject(body);
                        JSONObject jobj2 = jobj.getJSONObject("data");
                        temperature = jobj2.getString("temperature");
                        humidity = jobj2.getString("humidity");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                TextView textView1 = findViewById(R.id.wdz);
                                textView1.setText(temperature);
                                TextView textView2 = findViewById(R.id.sdz);
                                textView2.setText(humidity);
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

}
